package com.songVersion.controller;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.songVersion.controller.actions.DetailAction;
import com.songVersion.controller.actions.GeneralAction;
import com.songVersion.controller.actions.HomeAction;
import com.songVersion.controller.actions.SearchAction;

@SuppressWarnings("serial")
public class SongVersionServlet extends HttpServlet
{
	// private static final Logger log =
	// Logger.getLogger(SongVersionServlet.class.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException
	{
		resp.setContentType("text/plain");
		// log.info("servlet!");
		System.out.println("servlet!");
		try
		{
			System.out.println(req.getRequestURI());
			GeneralAction action = getAction(req);
			String view = action.process();
			System.out.println("s " + req.getAttribute("songName"));
			getServletContext().getRequestDispatcher(view).forward(req, resp);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException();
		}

	}

	private GeneralAction getAction(HttpServletRequest req)
	{
		String sAction = (String) req.getAttribute("action");
		System.out.println("action => " + sAction);
		GeneralAction action;
		if (sAction == null || "".equals(sAction))
		{
			action = new HomeAction();
		}else if ("search".equals(sAction))
		{
			action = new SearchAction();
		} else
		{
			action = new DetailAction();
		}
		action.init(req);
		return action;

	}

}
